<?php


namespace app\controller\api;


use app\common\service\sms\AliSms;
use app\util\ReturnCode;
use think\facade\Cache;
use think\Response;

class Captcha extends Base
{
    /**
     * @func send
     * @param phone 手机号
     * @method POST
     * @desc 发送验证码
     * @return response
     * @Time: 2021/12/6 17:39
     */
    public function send () : Response {
        $phone  = input('post.phone');
        if (empty($phone) || !validatePhone($phone)) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, '请输入正确的手机号');
        }
        if (Cache::has('code_time'.$phone) || Cache::get("code_time".$phone) > time()) {
            return $this->buildFailed(ReturnCode::SERVICE_ERROR, '请勿重复发送验证码');
        }
        $code = rand(100000, 999999);  // 长度
        return AliSms::send($phone, $code);
    }
}